
package PaqueteServlet;

import cl.tienda.modelo.Producto;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletCAgrCarrito2 extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
           
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletCAgrCarrito2</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletCAgrCarrito2 at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {      
        PrintWriter out = response.getWriter();
        Vector<Producto> productos = (Vector<Producto>) request.getSession().getAttribute("productos");
        int indice=Integer.parseInt(request.getParameter("indice"));
        Producto prod=productos.get(indice);

        out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Consultar Producto</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Consultar producto" + request.getContextPath() + "</h1>");
            out.println("<table align='center'>");
            out.println("<tr>");
            out.print("<td><a href='ServletCConProd.do'>Consultar Productos</a> | </td>");
            out.print("<td><a href='ServletCAgrCarrito1.view'>Agregar al Carrito</a> | </td>");
            out.print("<td><a href='ServletCFinalizarCompra.do'>Salir</a></td>");
            out.println("</tr>");
            out.println("</table>");
            
            out.println("<form action='ServletCAgrCarrito3.view' method='POST'>");
            out.println("Codigo:  <input type='text' name='txtIndice' value='"+indice+"' readonly='readonly' /></br></br>");
            out.println("Producto:  <input type='text' name'txtProducto' value='"+prod.getNombreProducto()+"' readonly='readonly' /></br></br>");
            out.println("Stock:  <input type='text' name='txtStock' value='"+prod.getStock()+"' readonly='readonly' /></br></br>");   
            out.println("Cantidad a Pedir:  <input type='text' name='txtCantidad' value='0' /></br></br>");
            out.println("<input type='submit' value='Solicitar' name='button' />");
            out.println("</form>");
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
            out.close();      
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>   
}

